package com.mlethe.library.utils.app;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;

/**
 * 状态栏工具类
 *
 * @author Mlethe
 * @date 2018/1/11
 */

public class StatusBarUtil {
    /**
     * 设置状态栏颜色
     *
     * @param activity
     * @param color
     */
    public static void backgroundColor(Activity activity, @ColorInt int color) {
        if (activity == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 代表 5.0 及以上
            Window window = activity.getWindow();
            // 添加Flag把状态栏设为可绘制模式
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            // 取消状态栏透明
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 设置状态栏颜色
            window.setStatusBarColor(color);
            //设置系统状态栏处于可见状态
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // versionCode > 4.4 and versionCode < 5.0
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 添加状态栏view
            createStatusBar(activity, color);
        }
    }

    /**
     * 创建一个需要填充的statusBarView
     *
     * @param activity
     * @return
     */
    private static View createStatusBarView(Activity activity, @ColorInt int color) {
        if (activity == null) {
            return null;
        }
        View statusBarView = new View(activity);
        LinearLayout.LayoutParams statusBarParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getHeight(activity));
        statusBarView.setLayoutParams(statusBarParams);
        statusBarView.setBackgroundColor(color);
        return statusBarView;
    }

    /**
     * 创建statusBar
     *
     * @param activity
     * @param color
     * @return
     */
    public static void createStatusBar(Activity activity, @ColorInt int color, ViewGroup parent) {
        // 在原来的位置上添加一个状态栏
        View statusBarView = createStatusBarView(activity, color);
        if (statusBarView != null && parent != null) {
            parent.addView(statusBarView, 0);
        }
    }

    /**
     * 创建statusBar
     *
     * @param activity
     * @param color
     */
    public static void createStatusBar(Activity activity, @ColorInt int color) {
        if (activity == null) {
            return;
        }
        // 留出高度 setFitsSystemWindows true代表会调整布局，会把状态栏的亮度留出来
        ViewGroup androidContainer = (ViewGroup) ((ViewGroup) activity.getWindow().getDecorView()).getChildAt(0);
        if (androidContainer != null) {
            androidContainer.setFitsSystemWindows(true);
            androidContainer.setClipToPadding(true);
        }
        // 在原来的位置上添加一个状态栏
        createStatusBar(activity, color, androidContainer);
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     */
    public static int getHeight(Context context) {
        if (context == null) {
            return 0;
        }
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 状态栏透明，整个界面全屏
     *
     * @param activity
     */
    public static void transparent(Activity activity) {
        if (activity == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 代表 5.0 及以上
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            return;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // versionCode > 4.4 and versionCode < 5.0
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 设置状态栏文字颜色及图标为 深色 或 浅色(7.0以上支持)
     *
     * @param activity
     * @param isDark   true -> 深色   false -> 浅色
     */
    public static void textColor(Activity activity, boolean isDark) {
        if (activity == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = activity.getWindow().getDecorView();
            int systemUiVisibility = decorView.getSystemUiVisibility();
            if (isDark) {
                //深色
                decorView.setSystemUiVisibility(systemUiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                //浅色
                decorView.setSystemUiVisibility(systemUiVisibility | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

}